package com.mohan.dlxj.controller;


import com.mohan.dlxj.common.R;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Description:
 * Version: V1.0
 */
@RestController
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/captcha")
    public R captcha() throws Exception {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        //生成图片验证码，verCode验证码的字符
        String verCode = specCaptcha.text().toLowerCase();
        //表示存储在redis中的key
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        redisTemplate.opsForValue().set(key,verCode,10, TimeUnit.MINUTES);
        R r = new R();
        r.setCode(200);
        r.setMsg("");
        Map map = new HashMap<>();
        map.put("key",key);
        map.put("image",specCaptcha.toBase64());
        // 将key和base64返回给前端
        r.setData(map);
        return r;
    }
}
